/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.dao;

import com.myapp.struts.actionform.LocationMasterFormBean;
import com.myapp.struts.prepare.PrepareLocationMaster;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.struts.action.ActionForm;

/**
 *
 * @author pavan
 */
public class LocationMasterDao {

    private Connect drv;
    private Connection con;
    private Vector vec;
    private String insertLocation;
    private PreparedStatement pstmt;
    private int status;
    private String locationQuery;
    private ResultSet locationsRs;
    private LocationMasterFormBean lmb;
    private PrepareLocationMaster plm;
    private String deleteLocation;
    private String updateLocation;
    private LocationMasterFormBean form1;

    public LocationMasterDao() {
        form1 = new LocationMasterFormBean();
        plm = new PrepareLocationMaster();
    }

    public boolean delete(LocationMasterFormBean lmb) {
        try {
            con = getConnect();
            deleteLocation = "delete from locationmaster where locationcode=?";
            pstmt = con.prepareStatement(deleteLocation);
            pstmt.setString(1, lmb.getLocationCode());
            status = pstmt.executeUpdate();
            con.close();
            pstmt.close();
            if (status >= 1) {
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            Logger.getLogger(LocationMasterDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public Connection getConnect() throws Exception {
        drv = new Connect();
        con = drv.mySql();
        vec = new Vector();
        return con;
    }

    public boolean insert(ActionForm form) {
        try {
            /** Type cast ActionForm variable to FormBean */
            form1 = (LocationMasterFormBean) form;

            /** Take Database Connection */
            con = getConnect();
            insertLocation = "insert into locationmaster values(?,?)";
            pstmt = con.prepareStatement(insertLocation);
            pstmt.setString(1, form1.getLocationCode());
            pstmt.setString(2, form1.getLocationName());
            status = pstmt.executeUpdate();
            con.close();
            pstmt.close();
            if (status >= 1) {
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            Logger.getLogger(LocationMasterDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public Vector getLocations() {
        try {
            con = getConnect();
            locationQuery = "select distinct(locationcode) from locationmaster";
            pstmt = con.prepareStatement(locationQuery);
            locationsRs = pstmt.executeQuery();
            vec = plm.prepareLocationCode(locationsRs);
            con.close();
            pstmt.close();
        } catch (Exception ex) {
            Logger.getLogger(LocationMasterDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return vec;
    }

    public Vector getRows() {
        try {
            con = getConnect();
            locationQuery = "select * from locationmaster";
            pstmt = con.prepareStatement(locationQuery);
            locationsRs = pstmt.executeQuery();
            vec = plm.prepareLocationMaster(locationsRs);
            con.close();
            pstmt.close();
        } catch (Exception ex) {
            Logger.getLogger(LocationMasterDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return vec;
    }

    public boolean update(LocationMasterFormBean lmb) {
        try {
            con = getConnect();

            updateLocation = "update locationmaster set locationname=? where locationcode=?";
            System.out.println("1-->" + lmb.getLocationName());
            System.out.println("2-->" + lmb.getLocationCode());
            pstmt = con.prepareStatement(updateLocation);
            pstmt.setString(1, lmb.getLocationName());
            pstmt.setString(2, lmb.getLocationCode());
            status = pstmt.executeUpdate();

            con.close();
            pstmt.close();
            if (status >= 1) {
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            Logger.getLogger(LocationMasterDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}
